package com.xnew.file.service.impl;

import com.xnew.file.service.FileCommonService;
import com.xnew.utils.RedisUtil;
import com.xnew.utils.StringUtils;
import com.xnew.bean.common.FileInfo;
import com.xnew.constant.Constant;
import com.xnew.file.mapper.FileCommonMapper;
import com.xnew.utils.IdUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 数据字典处理公共使用的service信息
 * @Author yangcd
 * @Date 2020/12/31
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class FileCommonServiceImpl implements FileCommonService {
    @Autowired
    private FileCommonMapper fileCommonMapper;
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    /**
     * 保存附件信息通用方法
     * @param businessType 业务类型
     * @param businessId 业务编号
     * */
    @Override
    public void saveFileInfo(String businessType, String businessId, String filePath) throws Exception {
        String id = IdUtils.getRandomIdByUUID();
        String fileName = RedisUtil.get(RedisUtil.buildKey(":", new String[]{Constant.FILE_PREFIX, filePath}));
        FileInfo fileInfo = new FileInfo(id, fileName, filePath, businessType, businessId, "");
        fileCommonMapper.saveFileInfo(fileInfo);
    }

    @Override
    public void batchSaveFileInfo(String businessType, String businessId, Object filePaths) throws Exception {
        if(null != filePaths){
            String[] filePath = filePaths.toString().split(";");
            for(int i=0; i< filePath.length; i++){
                if(!StringUtils.isEmpty(filePath[i])){
                    this.saveFileInfo(businessType, businessId, filePath[i]);
                }
            }
        }
    }

    /**
     * 查询附件信息通用方法
     * @param businessType 业务类型
     * @param businessId 业务编号
     * */
    @Override
    public List<FileInfo> selectAllBusinessFile(String businessType, String businessId) throws Exception {
        return fileCommonMapper.selectAllBusinessFile(businessType, businessId);
    }

    /**
     * 删除附件信息通用方法
     * @param filePath 文件路径
     * */
    @Override
    public void deleteFileByFilePath(String filePath, String userId) throws Exception {
        //new FileUtil().deleteFile(fileId, userId);
        fileCommonMapper.deleteFileByFilePath(filePath);
    }

    @Override
    public void deleteFileByBusiness(String businessType, String businessId, String userId) throws Exception {
        fileCommonMapper.deleteFileByBusiness(businessType, businessId);
//        List<FileInfo> fileList = this.selectAllBusinessFile(businessType, businessId);
//        for(FileInfo file : fileList){
//            this.deleteFileByFilePath(file.getFilePath(), userId);
//        }
    }

    @Override
    public List<FileInfo> selectByIds(String ids) {
        return fileCommonMapper.selectByIds(ids);
    }
}
